using System;
using System.Text;
using System.Windows.Forms;
using OpenNETCF.Telephony;

namespace CallForward
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : Form
    {
        private Button btnForward;
        private TextBox txtNumber;
        private Label label1;
        private MainMenu mainMenu1;
        private ForwardList lfl;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.btnForward = new System.Windows.Forms.Button();
            this.txtNumber = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // btnForward
            // 
            this.btnForward.Location = new System.Drawing.Point(8, 80);
            this.btnForward.Name = "btnForward";
            this.btnForward.Size = new System.Drawing.Size(72, 20);
            this.btnForward.TabIndex = 2;
            this.btnForward.Text = "Forward";
            this.btnForward.Click += new System.EventHandler(this.btnForward_Click);
            // 
            // txtNumber
            // 
            this.txtNumber.Location = new System.Drawing.Point(8, 40);
            this.txtNumber.Name = "txtNumber";
            this.txtNumber.Size = new System.Drawing.Size(224, 21);
            this.txtNumber.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(8, 8);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(168, 20);
            this.label1.Text = "Forwarding number:";
            // 
            // Form1
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.ClientSize = new System.Drawing.Size(240, 268);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txtNumber);
            this.Controls.Add(this.btnForward);
            this.Menu = this.mainMenu1;
            this.MinimizeBox = false;
            this.Name = "Form1";
            this.Text = "Call Forward Demo";
            this.ResumeLayout(false);

        }

        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        private static void Main()
        {
            Application.Run(new Form1());
        }

        private void btnForward_Click(object sender, EventArgs e)
        {
            // Initialize TAPI
            Telephony phone = new Telephony();
            phone.Initialize();
            
            // Get a reference to the cellular line from the phone stack
            Line line = phone.GetCellularLine(MediaMode.InteractiveVoice, CallPrivilege.None);

            
            // Query the address status of the cellular line
            AddressStatus status = new AddressStatus(2048);
            status.Store();
            NativeMethods.lineGetAddressStatus(line.hLine, 0, status.Data);
            status.Load();
            
            // Create a new forwarding list and populate with forwards that are already configured
            lfl = new ForwardList(status.ForwardSize);
            Buffer.BlockCopy(status.Data, status.ForwardOffset, lfl.Data, 0, status.ForwardSize);
            MessageBox.Show("populated forwardlist");
            
            // Display information about each forward
            lfl.NumEntries = status.ForwardNumEntries;
            for (int i = 0; i < lfl.NumEntries; i++)
            {
                LineForward lf = lfl.List[i];
                MessageBox.Show("loaded forward " + i.ToString());
                string number = Encoding.Unicode.GetString(status.Data, lf.DestAddressOffset, lf.DestAddressSize);
                MessageBox.Show(string.Format("Forward mode: {0}, number: {1}", lf.ForwardMode, number));
            }
            
            // Cleanup any TAPI references
            status.Store();
            line.Dispose();
            phone.Shutdown();
        }
    }
}